package com.gome.axwl.aoc.facade.impl;

import com.alibaba.fastjson.JSONObject;
import com.gome.axwl.aoc.common.util.BeanJsonUtil;
import com.gome.axwl.aoc.entity.dto.OrderSearchDto;
import com.gome.axwl.aoc.entity.dto.WaybillSearchDto;
import com.gome.axwl.aoc.entity.po.OrderSearchPo;
import com.gome.axwl.aoc.entity.po.WaybillSearchPo;
import com.gome.axwl.aoc.facade.OrderFacade;
import com.gome.axwl.aoc.service.OrderService;
import com.gome.commons.core.ResultData;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderFacadeImpl implements OrderFacade {

    private final static Logger log = LoggerFactory.getLogger(OrderFacadeImpl.class);

    @Autowired
    private OrderService orderService;


    /**
     * 模糊查询服务商
     *
     * @param keyword
     * @return
     */
    @Override
    public Object getCarrierByKeyword(String keyword, String dcId) {
        return orderService.getCarrierByKeyword(keyword, dcId);
    }

    /**
     * 分页查询服务商
     *
     * @param orderSearchDto
     * @return
     */
    @Override
    public ResultData getOrderList(OrderSearchDto orderSearchDto) {
        OrderSearchPo orderSearchPo = BeanJsonUtil.beanConvert(orderSearchDto, OrderSearchPo.class);
        return orderService.getOrderList(orderSearchPo);
    }

    /**
     * 根据订单号查询运单列表
     *
     * @param waybillSearchDto
     * @return
     */
    @Override
    public ResultData getWaybillByOrderNo(WaybillSearchDto waybillSearchDto) {
        WaybillSearchPo waybillSearchPo = BeanJsonUtil.beanConvert(waybillSearchDto, WaybillSearchPo.class);
        return orderService.getWaybillByOrderNo(waybillSearchPo);
    }

    /**
     * 查询订单详情
     *
     * @param orderNo
     * @return
     */
    @Override
    public ResultData getOrderDetail(String orderNo) {
        return orderService.getOrderDetail(orderNo);
    }

    /**
     * 查询运单详情
     *
     * @param waybillNo
     * @return
     */
    @Override
    public ResultData getWaybillDetail(String waybillNo) {
        return orderService.getWaybillDetail(waybillNo);
    }

    /**
     * 拒绝原因照片地址下载
     *
     * @param waybillNo
     * @return
     */
    @Override
    public JSONObject downloadRefuseFile(String waybillNo, HttpServletResponse response) {
        ResultData resultData = orderService.getWaybillDetail(waybillNo);
        JSONObject fileJson = new JSONObject();
        if (ResultData.isSuccess(resultData)) {
            Object object = resultData.getData();
            if (null != object) {
                Map beanMap = (HashMap)object;
                String urlStr = String.valueOf(beanMap.get("refuseFile"));
                if (!StringUtils.isEmpty(urlStr)) {
                    fileJson.put("urlStr", urlStr);
                    fileJson.put("fileName", String.valueOf(beanMap.get("thirdWaybillNo")) + "-拒签照片.zip");
                }

            } else {
                //转换失败
                log.warn("[downloadRefuseFile] getWaybillDetail:{} 返回结果无data数据", waybillNo, resultData);
            }

        } else {
            //下载失败
            log.warn("[downloadRefuseFile] getWaybillDetail:{} 返回结果失败", waybillNo, resultData);
        }
        log.info("downloadRefuseFile 返回结果:{}", fileJson);
        return fileJson;

    }


    /**
     * 状态字典查询
     *
     * @param code
     * @return
     */
    @Override
    public ResultData getStatus(String code) {
        return orderService.getStatus(code);
    }
}
